package com.tl.boot.main.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;

/**
 * com.tl.boot.main.configuration -- LocalAsyncConfig
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/12 9:35
 */
@Component
public class LocalAsyncConfig implements AsyncConfigurer {
    private static final Logger log = LoggerFactory.getLogger(LocalAsyncConfig.class);
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecuter = new ThreadPoolTaskExecutor();
        taskExecuter.setCorePoolSize(4);
        taskExecuter.setMaxPoolSize(120);
        taskExecuter.setQueueCapacity(25);
        taskExecuter.initialize();
        return taskExecuter;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new MyAsyncExceptionHandler();
    }
    class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
        //手动处理捕获的异常
        @Override
        public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
            log.debug("------------->>>捕获线程异常信息");
            log.info("Exception message - " + throwable.getMessage());
            log.info("Method name - " + method.getName());
            for (Object param : obj) {
                log.info("Parameter value - " + param);
            }
        }

    }
}


